package com.ranlen.ems_vue.controller;

import com.ranlen.ems_vue.entity.*;
import com.ranlen.ems_vue.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("emp")
@Slf4j
public class EmpController {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private EmpService empService;

    @Value("${photo.dir}")
    private String realPath;

    @GetMapping("findAll")
    public List<Emp> findAll(HttpServletRequest request){
        String token = (String) request.getSession().getAttribute("token");
        String key = stringRedisTemplate.opsForValue().get(token);
        User user = (User)redisTemplate.opsForValue().get(key);
        return empService.findAll2(user.getId());
    }
    @GetMapping("findAll2")
    public List<Emp> findAll2(){
        return empService.findAll();
    }

    @PostMapping("save")
    public Map<String,Object> save(Emp emp, MultipartFile photo,HttpServletRequest request) throws IOException {
        log.info("员工信息：[{}]",emp.toString());
        log.info("头像信息：[{}]",photo.getOriginalFilename());

        User_Emp user_emp;
        Map<String,Object> map = new HashMap<>();
        String token = (String) request.getSession().getAttribute("token");
        String key = stringRedisTemplate.opsForValue().get(token);
        User user = (User)redisTemplate.opsForValue().get(key);

        try {
            String newFileName = UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath,newFileName));

            emp.setPath(newFileName);
            empService.save(emp);

            user_emp = new User_Emp();
            user_emp.setE_id(emp.getId());
            user_emp.setU_id(user.getId());
            System.out.println("user_emp======"+user_emp);
            empService.save_relation(user_emp);

            map.put("state",true);
            map.put("msg","信息保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","信息保存失败");
        }

        return map;
    }
    @GetMapping("delete")
    public Map<String,Object> delete(String id){
        log.info("删除id：[{}]",id);
        Map<String,Object> map = new HashMap<>();
        try {
            Emp emp = empService.findEmpById(id);
            File file = new File(realPath,emp.getPath());
            if (file.exists())file.delete();

            empService.delete(id);

            empService.delete_relation(id);

            map.put("state",true);
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","删除成功");
        }
        return map;
    }
    @GetMapping("findOne")
    public Emp findOne(String id){
        log.info("查询的id：[{}]",id);
        Emp emp = empService.findEmpById(id);
        return emp;
    }
    @PostMapping("update")
    public Map<String,Object> update(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息：[{}]",emp.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (photo!=null&&photo.getSize()!=0){
                log.info("头像信息：[{}]",photo.getOriginalFilename());
                String newFileName = UUID.randomUUID().toString()+"."+ FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath,newFileName));
                File file = new File(realPath,emp.getPath());
                if (file.exists())file.delete();
                emp.setPath(newFileName);
            }
            empService.update(emp);
            map.put("state",true);
            map.put("msg","信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","信息更新失败");
        }

        return map;
    }
    @GetMapping("findSalaryAll")
    public List<Salary> findSalaryAll(HttpServletRequest request){
        String token = (String) request.getSession().getAttribute("token");
        String key = stringRedisTemplate.opsForValue().get(token);
        User user = (User)redisTemplate.opsForValue().get(key);
        List<Salary> salaryList = empService.findSalaryAll(user.getId());
        List<Kaoqin> kaoqinList = empService.findKaoqinAll(user.getId());
        Money money = empService.findMoneyAll(user.getId());
        for (int i = 0; i < salaryList.size(); i++) {
            salaryList.get(i).setOvertime(kaoqinList.get(i).getOvertimeN()*money.getOvertimeM());
            salaryList.get(i).setAllowance(kaoqinList.get(i).getLeaveN()*money.getLeaveM()-kaoqinList.get(i).getLateN()*money.getLateM());
        }
        return salaryList;
    }
    @GetMapping("findMoneyAll")
    public Money findMoneyAll(HttpServletRequest request){
        String token = (String) request.getSession().getAttribute("token");
        String key = stringRedisTemplate.opsForValue().get(token);
        User user = (User)redisTemplate.opsForValue().get(key);
        return empService.findMoneyAll(user.getId());
    }
    @GetMapping("findKaoqinAll")
    public List<Kaoqin> findKaoqinAll(HttpServletRequest request){
        String token = (String) request.getSession().getAttribute("token");
        String key = stringRedisTemplate.opsForValue().get(token);
        User user = (User)redisTemplate.opsForValue().get(key);
        return empService.findKaoqinAll(user.getId());
    }

    @GetMapping("findSalaryOne")
    public Salary findSalaryOne(String id){
        log.info("查询的id：[{}]",id);
        Salary salary = empService.findSalaryOne(id);
        return salary;
    }
    @GetMapping("findKaoqinOne")
    public Kaoqin findKaoqinOne(String id){
        log.info("查询的id：[{}]",id);
        Kaoqin kaoqin = empService.findKaoqinOne(id);
        return kaoqin;
    }
    @PostMapping("updateSalary")
    public Map<String,Object> updateSalary(Salary salary) throws IOException {
        log.info("工资信息：[{}]",salary.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            empService.updateSalary(salary);
            map.put("state",true);
            map.put("msg","信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","信息更新失败");
        }

        return map;
    }
    @PostMapping("updateMoney")
    public Map<String,Object> updateMoney(Money money) throws IOException {
        log.info("工资信息：[{}]",money.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            empService.updateMoney(money);
            map.put("state",true);
            map.put("msg","信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","信息更新失败");
        }

        return map;
    }
    @PostMapping("updateKaoqin")
    public Map<String,Object> updateKaoqin(Kaoqin kaoqin) throws IOException {
        log.info("工资信息：[{}]",kaoqin.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            empService.updateKaoqin(kaoqin);
            map.put("state",true);
            map.put("msg","信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state",false);
            map.put("msg","信息更新失败");
        }

        return map;
    }
}
